const {mysql} = require('../qcloud')

module.exports = async (ctx) => {
    const {id, openid} = ctx.request.query
    const commentSql = mysql('comments')
        .select('comments.*', 'csessioninfo.user_info')
        .join('csessioninfo', 'comments.openid', 'csessioninfo.open_id')
        // .where('bookid', id)
    console.log(commentSql)
    let comments
    if (id) {
        comments = await commentSql.where('bookid', id)
    } else if (openid) {
        comments = await commentSql.where('openid', openid)
    }
    ctx.state.data = {
        list: comments.map(v => {
            let info = JSON.parse(v.user_info)
            return Object.assign({}, v, {
                user_info: {
                    ninkName: info.nickName,
                    image: info.avatarUrl
                }
            })
        })
    }
}
